var app = getApp();
// pages/user/apply_team.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    age: ['选择年龄', '17岁以下', '18-24岁', '25-29岁', '30-39岁', '40-49岁', '50岁以上'],
    ageIndex: 0,
    work: ['选择职业', '自由', '企业', '单位', '个体', '学生', '退休'],
    workIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindAgeChange: function (e) {
    this.setData({
      ageIndex: e.detail.value
    })
  },
  bindWorkChange: function (e) {
    this.setData({
      workIndex: e.detail.value
    })
  },
  applyPost: function (e) {
    var that = this;
    var value = e.detail.value;

    var name = value.name;
    var mobile = value.mobile;
    var address = value.address;
    var age = that.data.age[value.age];
    var work = that.data.work[value.work];

    if (name == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        image: '',
        duration: 1500,
        mask: false
      });
      return;
    }

    if (mobile == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        image: '',
        duration: 1500,
        mask: false
      });
      return;
    }

    if (address == '') {
      wx.showToast({
        title: '请输入住址',
        icon: 'none',
        image: '',
        duration: 1500,
        mask: false
      });
      return;
    }
    if (that.data.ageIndex == 0) {
      wx.showToast({
        title: '请选择年龄',
        icon: 'none',
        image: '',
        duration: 1500,
        mask: false
      });
      return;
    }
    if (that.data.workIndex == 0) {
      wx.showToast({
        title: '请选择职业',
        icon: 'none',
        image: '',
        duration: 1500,
        mask: false
      });
      return;
    }

    var token = wx.getStorageSync("token");
    var reqTask = wx.request({
      url: app.apiUrl('user/apply_team'),
      data: { name: name, mobile: mobile, age: age, work: work, address: address },
      header: {
        'content-type': 'application/json',
        "X-ECTouch-Authorization": token
      },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: (result) => {
        if (result.data.code == 0) {
          wx.redirectTo({
            url: 'myinvite'
          });
        } else if (result.data.code == 1) {
          wx.redirectTo({
            url: 'myinvite'
          });

        } else {
          wx.showToast({
            title: result.data.data,
            icon: 'none',
            image: '',
            duration: 1500,
            mask: false
          });
        }
      },
      fail: () => { },
      complete: () => { }
    });

  }
})